package com.parkreport.admin.parkarea.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.common.base.BaseService;
import com.common.jdbc.DB4ObjUtil;
import com.common.jdbc.DBBaseUtil;
import com.common.jdbc.DBSource;
import com.common.pager.PageHelper;
import com.common.util.StringUtil;
import com.parkreport.vo.CarAreaVO;
import com.parkreport.vo.DmbVO;
import com.parkreport.vo.ParkArea;
import com.parkreport.vo.ParkDmbVO;

public class ParkAreaService extends BaseService {
	
	public List find(ParkArea vo,PageHelper pager){
		String sql = "select * from PARK_AREA where 1=1 and DEL_FLAG='0' ";
		List<ParkArea> list = DB4ObjUtil.executePager4Obj(new ParkArea(), sql,pager);
		
		return list;
	}
	
	public List find(String pid,ParkArea vo,PageHelper pager){
		StringBuilder sql = new StringBuilder( "select * from PARK_AREA where 1=1 and DEL_FLAG='0' and PID=? ");
		if(vo!=null){
			if(!StringUtil.isBlank(vo.getAreaName())){
				sql.append(" and AREA_NAME like '%"+vo.getAreaName()+"%'");
			}
		}
		List<ParkArea> list = DB4ObjUtil.executePager4Obj(new ParkArea(), sql.toString(),pager,pid);
		
		return list;
	}
	
	public List find(){
		String sql = "select * from PARK_AREA where 1=1 and DEL_FLAG='0' ";
		List<ParkArea> list = DB4ObjUtil.execute4Obj(new ParkArea(), sql);
		
		return list;
	}
	
	public Map findMap(){
		Map parkAreaMap = new HashMap();
		String sql = "select * from PARK_AREA where 1=1 and DEL_FLAG='0' ";
		List<ParkArea> list = DB4ObjUtil.execute4Obj(new ParkArea(), sql);
		for(int i=0;i<list.size();i++){
			parkAreaMap.put(list.get(i).getId(), list.get(i).getAreaName());
		}
		parkAreaMap.put("0", "顶级");
		return parkAreaMap;
	}
	
	public ParkArea get(String id){
		String sql = "select * from PARK_AREA where 1=1 and ID=?";
		List<ParkArea> list = DB4ObjUtil.execute4Obj(new ParkArea(), sql, id);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}
	

	public void delete(String ids){
		//String sql = "delete from PARK_AREA where CarAreaID=?";
		String sql = "update PARK_AREA set DEL_FLAG='1' where ID=?";
		if(!StringUtil.isBlank(ids)){
			String[] idArr = ids.split(",");
			DBBaseUtil.executeBatchUpdate(sql, idArr);
			
		}
	}
	
	public void delete(String[] ids){
		//String sql = "delete from PARK_AREA where CarAreaID=?";
		String sql = "update PARK_AREA set DEL_FLAG='1' where ID=?";
		if(!StringUtil.isBlank(ids)){
			DBBaseUtil.executeBatchUpdate(sql, ids);
		}
	}
	
	public String getCode(String pid){
		String sql = "select max(AREA_CODE) from PARK_AREA where 1=1 and PID=? ";
		String obj = (String) DB4ObjUtil.getObjectSingle(sql, pid);
		String newCode="001";
		if(!StringUtil.isBlank(obj)){
			int code = StringUtil.Str2Int(obj);
			 newCode = StringUtil.keeperStr(String.valueOf(code+1), obj.length(), "0");
			
		}else{
			ParkArea area = get(pid);
			if(area!=null){
				newCode = area.getAreaCode()+"001";
			}
		}
		log.info("maxcode===="+newCode);
		return newCode;
	}
	
	public String save(ParkArea vo,List<ParkDmbVO> dmbList){
		if(vo!=null){
			String isHave = isApply(vo);
			if("0".equals(isHave)){
				Connection conn = DBSource.getConnection();
				
				try{
					conn.setAutoCommit(false);
				if(StringUtil.isBlank(vo.getId())){
						String id = StringUtil.getUuid();
						String sql = "insert into PARK_AREA(ID,AREA_NAME,AREA_CODE,PID,DEL_FLAG) values(?,?,?,?,?)";
						DBBaseUtil.executeUpdateByConn(conn,sql, id,vo.getAreaName(),getCode(vo.getPid()),vo.getPid(),"0");
						vo.setId(id);
				}else{
					
					String sql = "update PARK_AREA set AREA_NAME=?  where ID=?";
					DBBaseUtil.executeUpdateByConn(conn,sql, vo.getAreaName(),vo.getId());
				}
				DmbService dmbService = new DmbService();
				dmbService.saveList(conn,dmbList, vo.getId());
				conn.commit();
				}catch(Exception e){
					log.error(e.getMessage(),e);
					try {
						conn.rollback();
					} catch (SQLException e1) {
						log.error(e.getMessage(),e1);
					}
					return "0";
				}finally{
					try {
						conn.close();
					} catch (SQLException e) {
						log.error(e.getMessage(),e);
					}
				}
			}else{
				return "0";
			}
		}
		return "1";
	}
	
	public String isApply(ParkArea vo){
		String sql = "select * from PARK_AREA where 1=1 and AREA_NAME=? and ID<>?";
		List<CarAreaVO> list = DB4ObjUtil.execute4Obj(new ParkArea(), sql, vo.getAreaName(),vo.getId());
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	public List findPark(){
		String sql = "select * from PARK_AREA where 1=1 and DEL_FLAG='0' and pid<>'0' ";
		List<ParkArea> list = DB4ObjUtil.execute4Obj(new ParkArea(), sql);
		
		return list;
	}
	public String getParkTree(){
		List<ParkArea> list = findPark();
		
		Document doc = DocumentHelper.createDocument();
		Element tree = doc.addElement("item");
		tree.addAttribute("id","0");
		for (int i = 0; i < list.size(); i++) {
			Element e = tree.addElement("item");
			 e.addAttribute("id", String.valueOf(list.get(i).getId()));
			 e.addAttribute("text", list.get(i).getAreaName());
			
			
		}
		
		
		
		
		
		return doc.asXML();
	}
}
